USE [MCESeries]
GO
/****** Object:  ForeignKey [FK_Episode_Season]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Episode_Season]') AND parent_object_id = OBJECT_ID(N'[Episode]'))
ALTER TABLE [Episode] DROP CONSTRAINT [FK_Episode_Season]
GO
/****** Object:  ForeignKey [FK_Season_Series]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Season_Series]') AND parent_object_id = OBJECT_ID(N'[Season]'))
ALTER TABLE [Season] DROP CONSTRAINT [FK_Season_Series]
GO
/****** Object:  Table [dbo].[Episode]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Episode]') AND type in (N'U'))
DROP TABLE [Episode]
GO
/****** Object:  StoredProcedure [dbo].[series_get_all]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[series_get_all]') AND type in (N'P', N'PC'))
DROP PROCEDURE [series_get_all]
GO
/****** Object:  StoredProcedure [dbo].[series_get_by_id]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[series_get_by_id]') AND type in (N'P', N'PC'))
DROP PROCEDURE [series_get_by_id]
GO
/****** Object:  StoredProcedure [dbo].[series_save]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[series_save]') AND type in (N'P', N'PC'))
DROP PROCEDURE [series_save]
GO
/****** Object:  Table [dbo].[Season]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Season]') AND type in (N'U'))
DROP TABLE [Season]
GO
/****** Object:  Table [dbo].[Series]    Script Date: 06/29/2008 14:49:47 ******/
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Series]') AND type in (N'U'))
DROP TABLE [Series]
GO
/****** Object:  Table [dbo].[Series]    Script Date: 06/29/2008 14:49:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Series]') AND type in (N'U'))
BEGIN
CREATE TABLE [Series](
	[SeriesID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[TVDB_SeriesID] [int] NULL,
 CONSTRAINT [PK_Series] PRIMARY KEY CLUSTERED 
(
	[SeriesID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[Season]    Script Date: 06/29/2008 14:49:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Season]') AND type in (N'U'))
BEGIN
CREATE TABLE [Season](
	[SeasonID] [int] IDENTITY(1,1) NOT NULL,
	[SeasonNumber] [int] NOT NULL,
	[SeriesID] [int] NOT NULL,
 CONSTRAINT [PK_Season] PRIMARY KEY CLUSTERED 
(
	[SeasonID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  Table [dbo].[Episode]    Script Date: 06/29/2008 14:49:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[Episode]') AND type in (N'U'))
BEGIN
CREATE TABLE [Episode](
	[EpisodeID] [int] IDENTITY(1,1) NOT NULL,
	[EpisodeNumber] [int] NOT NULL,
	[Name] [nvarchar](255) NOT NULL,
	[SeasonID] [int] NOT NULL,
	[TVDB_EpisodeID] [int] NULL,
 CONSTRAINT [PK_Episode] PRIMARY KEY CLUSTERED 
(
	[EpisodeID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
END
GO
/****** Object:  StoredProcedure [dbo].[series_get_all]    Script Date: 06/29/2008 14:49:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[series_get_all]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		Jurjen A. Ladenius
-- Create date: 2008-06-29
-- Description:	Gets all series
-- =============================================
CREATE PROCEDURE [series_get_all] 
AS
BEGIN
	SET NOCOUNT ON;

	SELECT     
			SeriesID, 
			Name, 
			TVDB_SeriesID
	FROM         
			Series

END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[series_get_by_id]    Script Date: 06/29/2008 14:49:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[series_get_by_id]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		Jurjen A. Ladenius
-- Create date: 2008-06-28
-- Description:	Gets a series by id
-- =============================================
CREATE PROCEDURE [series_get_by_id] 
	@SeriesID int
AS
BEGIN
	SET NOCOUNT ON;

	SELECT     
			SeriesID, 
			Name, 
			TVDB_SeriesID
	FROM         
			Series
	WHERE
			SeriesID = @SeriesID

END
' 
END
GO
/****** Object:  StoredProcedure [dbo].[series_save]    Script Date: 06/29/2008 14:49:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[series_save]') AND type in (N'P', N'PC'))
BEGIN
EXEC dbo.sp_executesql @statement = N'-- =============================================
-- Author:		Jurjen A. Ladenius
-- Create date: 2008-06-28
-- Description:	Saves a series
-- =============================================
CREATE PROCEDURE [series_save] 
	-- Add the parameters for the stored procedure here
	@SeriesID int OUTPUT, 
	@Name nvarchar(255),
	@TVDB_SeriesID int
AS
BEGIN
	SET NOCOUNT ON;

	IF (@SeriesID is null) 
		BEGIN
		-- Insert
		INSERT INTO Series 
			(Name, TVDB_SeriesID)
		VALUES
			(@Name, @TVDB_SeriesID)
		SET @SeriesID = scope_identity()
		END
	ELSE
		BEGIN
		-- Update
		UPDATE Series
		SET
			Name = @Name,
			TVDB_SeriesID = @TVDB_SeriesID
		WHERE
			SeriesID = @SeriesID
		END

	SELECT @SeriesID
	RETURN @SeriesID
END
' 
END
GO
/****** Object:  ForeignKey [FK_Episode_Season]    Script Date: 06/29/2008 14:49:47 ******/
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Episode_Season]') AND parent_object_id = OBJECT_ID(N'[Episode]'))
ALTER TABLE [Episode]  WITH CHECK ADD  CONSTRAINT [FK_Episode_Season] FOREIGN KEY([SeasonID])
REFERENCES [Season] ([SeasonID])
ON DELETE CASCADE
GO
ALTER TABLE [Episode] CHECK CONSTRAINT [FK_Episode_Season]
GO
/****** Object:  ForeignKey [FK_Season_Series]    Script Date: 06/29/2008 14:49:47 ******/
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[FK_Season_Series]') AND parent_object_id = OBJECT_ID(N'[Season]'))
ALTER TABLE [Season]  WITH CHECK ADD  CONSTRAINT [FK_Season_Series] FOREIGN KEY([SeriesID])
REFERENCES [Series] ([SeriesID])
ON DELETE CASCADE
GO
ALTER TABLE [Season] CHECK CONSTRAINT [FK_Season_Series]
GO
